Ext.define('App.authorization.user.view.UserList', {
    extend: 'Extend.grid.CrudGridPanel',
    alias: 'widget.UserList',
    requires: ['App.authorization.user.UserViewModel'],
    viewModel: 'UserViewModel',
    config: {
        tbar: {
            quickCreate: {
                hidden: true
            },
            importUser: {
                text: '导入用户',
                handler: function () {
                    var me = this.up('CrudGridPanel');
                    Ext.create('Extend.window.SelectionWindow', {
                        title: '选择用户',
                        height: 500,
                        grid: 'App.hrm.employee.field.EmployeeSelectionWindow',
                        listeners: {
                            selected: function (rs) {
                                me.importEmployeeToUser(rs);
                            }
                        }
                    }).show();
                }
            }
        }
    },
    bind: {
        store: '{Query}',
        columns: '{columns}',
        search: '{search}'
    },
    editor: {
        model: 'App.authorization.user.UserModel',
        formClazz: 'App.authorization.user.view.UserEditor',
        save: 'management/authorization/user/save_user',
        get: 'authorization/user/get_by_id',
        del: 'management/authorization/user/deletes',
        dataType: 'User'
    },
    addHandler: function (data) {
        if (Ext.isEmpty(data)) {
            data = {};
        }
        Ext.apply(data, {isNew: true, systemuser: true});
        this.editHandler(null, null, data);
    },
    importEmployeeToUser: function (val, flag) {
        var me = this;
        var record = val[0];
        var data = {};
        data.username = record.data.huaweiNumber;
        data.name = record.data.name;
        data.chinasoftNumber = record.data.chinasoftNumber;
        data.accountExpiringDate = Ext.Date.add(new Date(), Ext.Date.MONTH, 6);
        data.credentialsExpiringDate = Ext.Date.add(new Date(), Ext.Date.MONTH, 6);
        data.locked = false;
        data.systemuser = false;
        data.isNew = true;
        var record = Ext.create(me.editor.model, data);
        Ext.create('App.authorization.user.view.UserEditor', {
            url: 'management/authorization/user/save_user',
            targetGrid: me,
            isImport: true
        }).show().loadRecord(record);
    }
});